const { getAllPetList, getPetList, getPetListOfIsNull, getPetListOfStatus, adoptRelease, vetoRelease, getPetListLimitNull, getPetListLimitStatus } = require("../models/petModel")
var a = "all";
module.exports = {
  // 获取发布宠物信息列表
  getPetList: async (ctx) => {
    // 默认值
    let current = 1;
    let pageSize = 10;
    // 如果传了current参数就重新赋值
    if (ctx.request.query.current) {
      current = parseInt(ctx.request.query.current)
    }
    // 如果传了pageSize参数就重新赋值
    if (ctx.request.query.pageSize) {
      pageSize = parseInt(ctx.request.query.pageSize)
    }

    // 获取全部用户
    let all

    // 获取发布宠物信息列表
    let res;
    if (a == "all") {
      res = await getPetList(current, pageSize);
      all = await getAllPetList();
    } else if (a == "null") {
      res = await getPetListLimitNull(current, pageSize);
      all = await getPetListOfIsNull();
    } else {
      res = await getPetListLimitStatus(current, pageSize);
      all = await getPetListOfStatus();
    }

    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res,
        total: all.length
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "暂无数据",
        data: [],
        total: 0,
        current: current,
        pages: current - 1
      }
    }
  },
  // 查询宠物信息列表
  queryPetList: async (ctx) => {
    const { pet_release_status } = ctx.request.query;
    // 存储发布状态
    a = pet_release_status;
    let res;
    // 如果参数是空字符串则查询全部数据
    if (pet_release_status == "all") {
      res = await getPetList();
    } else if (pet_release_status === "null") { // 如果是null就查询为null的数据
      res = await getPetListOfIsNull();
    } else {
      res = await getPetListOfStatus(pet_release_status);
    }

    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res,
        total: res.length
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "暂无数据",
        data: [],
      }
    }
  },
  // 通过宠物发布审核
  adoptRelease: async (ctx) => {
    const { id } = ctx.request.body;
    // 通过宠物发布审核
    let res = await adoptRelease(id);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "已通过"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "失败"
      }
    }
  },
  // 否决宠物发布审核
  vetoRelease: async (ctx) => {
    const { id } = ctx.request.body;
    // 通过宠物发布审核
    let res = await vetoRelease(id);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "已通过"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "失败"
      }
    }
  }
}